package com;

import com.yidong.entity.YiDongObj;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.*;

import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Iterator;

/**
 * 移动 word excel 通用抽象方法
 */
public abstract  class YDCommonUtils {

    public static String getTime(){
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 定义日期时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
        // 格式化当前时间
        String formattedTime = now.format(formatter);
        return formattedTime;
    }

    //处理word
    public void wordUtils(YiDongObj obj,String inPath,String outPath,String fileName,String type){
        inPath = inPath + fileName;
        //读取word内容
        try (InputStream is = new FileInputStream(inPath);
             XWPFDocument document = new XWPFDocument(is)) {

            //遍历文档中的所有段落
            for (XWPFParagraph paragraph : document.getParagraphs()) {
                for (XWPFRun run : paragraph.getRuns()) {
                    String runText = run.text();

//                    System.out.println(runText);

                    runText = this.changeText(runText,obj);
                    run.setText(runText, 0);
                }
            }

            // 遍历文档中的所有表格
            for (XWPFTable table : document.getTables()) {
                for (XWPFTableRow row : table.getRows()) {
                    for (XWPFTableCell cell : row.getTableCells()) {
                        for (XWPFParagraph cellParagraph : cell.getParagraphs()) {
                            for (XWPFRun cellRun : cellParagraph.getRuns()) {
                                String runText = cellRun.text();

//                                System.out.println(runText);

                                runText = this.changeText(runText,obj);
                                cellRun.setText(runText, 0);
                            }
                        }
                    }
                }
            }

            // 保存文档到新文件
            String outPutName = outPath + getTime() + obj.getXmmc() + type + fileName ;
            try (OutputStream out = new FileOutputStream(outPutName)) {
                document.write(out);
            }
            System.out.println("文档已更新并保存为: " + outPutName);

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    //处理excel
    public void excelUtils(YiDongObj obj,String inPath,String outPath,String fileName){
        inPath = inPath + fileName;

        try (FileInputStream fis = new FileInputStream(inPath);
             Workbook workbook = new XSSFWorkbook(fis)) {
            Iterator<Sheet> sheetIterator =  workbook.sheetIterator();
            while(sheetIterator.hasNext()){
                Sheet sheet = sheetIterator.next();
                for (Row row : sheet) {
                    for (Cell cell : row) {
                        // 只在文本类型的单元格中查找和替换
                        if (cell.getCellType() == CellType.STRING) {
                            String originalText = cell.getStringCellValue();
                            String runText = originalText;

                            //替换
                            runText = this.changeText(runText,obj);
                            // 如果替换后的文本与原始文本不同，则更新单元格
                            if (!originalText.equals(runText)) {
                                cell.setCellValue(runText);
                            }
                        }
                    }
                }
            }

//            Sheet sheet = workbook.getSheetAt(0); // 获取第一个Sheet，索引从0开始

            String outPutName = outPath + getTime() + obj.getXmmc() + fileName ;
            // 将修改后的Workbook写入到新文件
            try (FileOutputStream fos = new FileOutputStream(outPutName)) {
                workbook.write(fos);
                System.out.println("Excel文件已成功保存为: " + outPutName);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public abstract String changeText(String runText,YiDongObj obj);

}
